package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TbAppVersion;
import com.ruoyi.system.domain.dto.TbDataPackageCreateDTO;
import com.ruoyi.system.domain.vo.TbDataPackageVO;
import com.ruoyi.system.domain.vo.TbDeviceInfoVO;
import com.ruoyi.system.domain.vo.TbDeviceVO;
import com.ruoyi.system.domain.vo.TbTaskDetailVO;
import com.ruoyi.system.mapper.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.TbDataPackage;
import com.ruoyi.system.service.ITbDataPackageService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 数据包信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Service
public class TbDataPackageServiceImpl implements ITbDataPackageService {
    @Autowired
    private TbDataPackageMapper tbDataPackageMapper;


    @Resource
    private TbDeviceAppsMapper tbDeviceAppsMapper;


    @Resource
    private TbDeviceInfoMapper tbDeviceInfoMapper;


    @Resource
    private TbAppVersionMapper tbAppVersionMapper;

    @Resource
    private TbTaskDetailMapper tbTaskDetailMapper;




    /**
     * 查询数据包信息
     *
     * @param id 数据包信息主键
     * @return 数据包信息
     */
    @Override
    public TbDataPackage selectTbDataPackageById(Long id) {
        return tbDataPackageMapper.selectTbDataPackageById(id);
    }

    /**
     * 查询数据包信息列表
     *
     * @param tbDataPackage 数据包信息
     * @return 数据包信息
     */
    @Override
    public List<TbDataPackage> selectTbDataPackageList(TbDataPackageVO tbDataPackage) {
        return tbDataPackageMapper.selectTbDataPackageList(tbDataPackage);
    }

    /**
     * 新增数据包信息
     *
     * @param
     * @return 结果
     */
    @Override
    public R insertTbDataPackage(TbDataPackageCreateDTO dto) {
        //查询数据包信息,不能有重复数据
        TbDataPackage historyDatapackage = tbDataPackageMapper.selectByPackageNameAndVersion(dto.getBundleName(), dto.getVersionCodeNum(), dto.getVersionCode());
        if (historyDatapackage != null){
           return R.fail("数据包已存在");
        }
        TbDataPackage dataPackage = new TbDataPackage();
        dataPackage.setPackageName(dto.getBundleName());
        dataPackage.setPackageVersionnumber(dto.getVersionCodeNum());
        dataPackage.setPackageVersioncode(dto.getVersionCode());
        dataPackage.setPackageSize(dto.getFileSize());
        dataPackage.setPackageHash(dto.getPackageHash());
        dataPackage.setPackageUrl(dto.getPackageUrl());
        dataPackage.setStatus(1);
        dataPackage.setPublishDate(new Date());
        //   dataPackage.setApplicableModels(dto.getDeviceTypes().toString());
        dataPackage.setIsForceUpdate(dto.getForceUpdate());
        dataPackage.setDevSupplier(dto.getVendor());
        dataPackage.setRemark(dto.getDescription());
        dataPackage.setCreateBy(getUsername());
        dataPackage.setCreateTime(new Date());
        dataPackage.setCreateTime(DateUtils.getNowDate());
        return R.ok(tbDataPackageMapper.insertTbDataPackage(dataPackage));
    }

    /**
     * 修改数据包信息
     *
     * @param tbDataPackage 数据包信息
     * @return 结果
     */
    @Override
    public int updateTbDataPackage(TbDataPackage tbDataPackage) {
        tbDataPackage.setUpdateTime(DateUtils.getNowDate());
        return tbDataPackageMapper.updateTbDataPackage(tbDataPackage);
    }

    /**
     * 批量删除数据包信息
     *
     * @param ids 需要删除的数据包信息主键
     * @return 结果
     */
    @Override
    public int deleteTbDataPackageByIds(Long[] ids) {
        return tbDataPackageMapper.deleteTbDataPackageByIds(ids);
    }

    /**
     * 删除数据包信息信息
     *
     * @param id 数据包信息主键
     * @return 结果
     */
    @Override
    public int deleteTbDataPackageById(Long id) {
        return tbDataPackageMapper.deleteTbDataPackageById(id);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        return tbDataPackageMapper.updateStatus(id, status);
    }

//    @Override
//    public List<TbDeviceInfoVO> selectBypackageVersioncode(String packageVersioncode) {
//        List<String> deviceSns = tbDeviceAppsMapper.selectTbDeviceSnBy(packageVersioncode);
//        if (CollectionUtils.isEmpty(deviceSns)) {
//            return Collections.emptyList();
//        }
//        List<TbDeviceInfoVO> list = tbDeviceInfoMapper.selectBypackageVersioncode(deviceSns);
//        return list;
//    }

    @Override
    public List<TbTaskDetailVO> listTaskBypackageVersioncode(String packageVersioncode) {
//        Long versionId = tbDataPackageMapper.selectapackageBy(packageVersioncode);
//        if (versionId == null) {
//            return Collections.emptyList();
//        }
//
//        List<TbTaskDetailVO> taskDetails   = tbTaskDetailMapper.selectTbTaskDetailListByPackageId(versionId);
//
//        if (CollectionUtils.isEmpty(taskDetails)) {
//            return Collections.emptyList();
//        }

        return null;
    }
}
